package persistence.impl;

import domain.Log;
import persistence.DBUtil;
import persistence.LogDAO;

import java.sql.Connection;
import java.sql.PreparedStatement;

public class LogDAOImpl implements LogDAO {
    private static final String ADD_LOG =
            "INSERT INTO `log` (action,time,user_id) VALUES(?,?,?)";

    @Override
    public boolean addLog(Log log) {
        boolean flag = false;
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(ADD_LOG);
            preparedStatement.setString(1, log.getAction());
            preparedStatement.setTimestamp(2, log.getTime());
            preparedStatement.setString(3, log.getUserid());
            int row = preparedStatement.executeUpdate();
            if(row == 1){
                flag = true;
            }
            DBUtil.closeStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }
}
